/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.gui.client;

//JAVA Packages
import java.util.*;

//GWTEXT Packages
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.*;
import com.gwtext.client.widgets.layout.*;

public class AnalysisPanelGroup extends Panel
// public class AnalysisPanelGroup extends
// com.google.gwt.user.client.ui.FlexTable
{
    NetFSEClient client = null;

    SearchAnalysisTab searchAnalysisTab;

    List activePanelList = new ArrayList();

    Panel mainPanel;

    Panel splashPanel = new Panel();

    public TabPanel tabPanel = null;

    public AnalysisPanelGroup(NetFSEClient client) {
        this.client = client;
        this.setTitle("Analysis");

        this.setLayout(new com.gwtext.client.widgets.layout.FitLayout());

        mainPanel = new Panel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBorder(false);

        tabPanel = new TabPanel();

        tabPanel.setResizeTabs(true);
        // Problem drawing the window when the tabs are on the bottom. For some
        // reason the tabs are slightly hidden and The selected query tab is not
        // activated.
        // tabPanel.setTabPosition(Position.BOTTOM);
        tabPanel.setMinTabWidth(80);
        tabPanel.setTabWidth(100);
        tabPanel.setEnableTabScroll(true);

        splashPanel.setTitle("Intro");
        splashPanel
                .setHtml("To view Search Results, perform a search using the Search tab or click on a Search Result ID in the Search Results section of the Search Info tab.");
        tabPanel.add(splashPanel);
        tabPanel.setActiveTab(0);

        mainPanel.add(tabPanel, new BorderLayoutData(com.gwtext.client.core.RegionPosition.CENTER));
        this.add(mainPanel);

        client.checkIfReadyToBuild(this);
    }

    public AnalysisPanelGroup(NetFSEClient client, SearchAnalysisTab searchAnalysisTab) {
        this.setTitle("Analysis");
        this.client = client;

        mainPanel = new Panel();
        mainPanel.setBorder(false);

        splashPanel.setTitle("Intro");
        splashPanel
                .setHtml("To view Search Results, perform a search using the Search tab or click on a Search Result ID in the Search Results section of the Search Info tab.");
        tabPanel.add(splashPanel);
        tabPanel.setActiveTab(0);

        this.searchAnalysisTab = searchAnalysisTab;
        tabPanel.setResizeTabs(true);
        // tabPanel.setTabPosition(Position.BOTTOM);
        tabPanel.setMinTabWidth(115);
        tabPanel.setTabWidth(135);
        tabPanel.setEnableTabScroll(true);
    }

    public void buildPanel() {

       
        splashPanel.setTitle("Intro");
        splashPanel
                .setHtml("To view Search Results, perform a search using the Search tab or click on a Search Result ID in the Search Results section of the Search Info tab.");
        tabPanel.add(splashPanel);
        tabPanel.setActiveTab(0);

        // this.add(tabPanel);
        // this.setWidget(0,0, tabPanel);
    }

    public void addQueryTabPanels(String[] queryIds) {

        tabPanel.remove(splashPanel, true);
        for (int x = 0; x < queryIds.length; x++) {
            boolean found = false;
            Component[] components = tabPanel.getItems();
            for (int y = 0; y < components.length; y++) {
                Panel panel = (Panel) components[y];
                if (panel.getTitle().equals(queryIds[x])) {
                    found = true;
                    //System.out.println("Found " + queryIds[x] + " in active list. Not opening.");
                    break;
                }
            }

            if (!found) {
                //System.out.println("Did not find " + queryIds[x] + " in active list. Opening.");
                AnalysisPanel analysisPanel = new AnalysisPanel(client, queryIds[x], searchAnalysisTab);
                analysisPanel.openQuery(queryIds[x]);
            }
        }

    }

    public void addQueryTabPanel(String queryId, int[] types) {
        tabPanel.remove(splashPanel, true);

        boolean found = false;
        Component[] components = tabPanel.getItems();
        for (int y = 0; y < components.length; y++) {
            Panel panel = (Panel) components[y];
            if (panel.getTitle().equals(queryId)) {
                found = true;
                //System.out.println("Found " + queryId + " in active list. Not opening.");
                break;
            }
        }

        if (!found) {
            AnalysisPanel analysisPanel = new AnalysisPanel(client, queryId, searchAnalysisTab, types);
            analysisPanel.openQuery(queryId);
            analysisPanel.setAutoScroll(true);
        }

    }

    public void addPanelToTab(Panel targetPanel) {
        final Panel tempPanel = targetPanel;

        targetPanel.setClosable(true);

        tabPanel.add(targetPanel);
        try {
            // targetPanel.setAutoScroll(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        tabPanel.activate(0);
    }

}
